/**
 * Url处理
 */
class UrlFn {

    /**
     * @desc   url参数转对象
     * @param  {String} url  default: window.location.href
     * @return {Object}
     */
    static parseQueryString(url) {
        url = !url ? window.location.href : url;
        if(url.indexOf('?') === -1) {
            return {};
        }
        let search = url[0] === '?' ? url.substr(1) : url.substring(url.lastIndexOf('?') + 1);
        if (search === '') {
            return {};
        }
        search = search.split('&');
        let query = {};
        for (let i = 0; i < search.length; i++) {
            let pair = search[i].split('=');
            query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
        }
        return query;
    }

    /**
     *
     * @desc   对象序列化为请求参数
     * @param  {Object} obj
     * @return {String}
     */
    static stringfyQueryString(obj) {
        if (!obj) return '';
        let pairs = [];

        for (let key in obj) {
            let value = obj[key];

            if (value instanceof Array) {
                for (let i = 0; i < value.length; ++i) {
                    pairs.push(encodeURIComponent(key + '[' + i + ']') + '=' + encodeURIComponent(value[i]));
                }
                continue;
            }

            pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
        }

        return pairs.join('&');
    }
}

module.exports = UrlFn;